//
//  XiaoximenPostViewController.m
//  Xiaoximen_iOS
//
//  Created by Wang Zhongyu on 12-7-19.
//  Copyright (c) 2012年 Peking University. All rights reserved.
//

#import "XiaoximenPostViewController.h"

@interface XiaoximenPostViewController () {
	BOOL _isKeyboardHidden;
	CGRect originalTextViewFrame;
}

@end

@implementation XiaoximenPostViewController
@synthesize textView;
@synthesize textImage;
@synthesize textLeftImage;
@synthesize message;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	self.textView.text = message;
}

- (void)viewDidUnload
{
	[self setTextView:nil];
	[self setTextImage:nil];
	[self setTextLeftImage:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
	[self.textView becomeFirstResponder];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (!CGRectContainsPoint(self.textView.frame,[touch locationInView:self.view])) {
        if (!_isKeyboardHidden) {
            [self.textView resignFirstResponder];
        }
    }
}
- (void)viewWillAppear:(BOOL)animated {
    // Register notifications for when the keyboard appears 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification*)notification {
	_isKeyboardHidden = NO;
    [self moveTextViewForKeyboard:notification up:YES];
}

- (void)keyboardWillHide:(NSNotification*)notification {
	_isKeyboardHidden = YES;
    [self moveTextViewForKeyboard:notification up:NO];
}

- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up {
    NSDictionary *userInfo = [notification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardRect;
	
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
	
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];
	
	if (up == YES) {
        CGFloat keyboardTop = keyboardRect.origin.y;
        CGRect newTextViewFrame = textImage.frame;
        originalTextViewFrame = textImage.frame;
		newTextViewFrame.size.height = keyboardTop - textImage.frame.origin.y - 20;
		
		[textLeftImage setFrame:CGRectMake(textLeftImage.frame.origin.x , textLeftImage.frame.origin.y, textLeftImage.frame.size.width, newTextViewFrame.size.height)];
		
        textImage.frame = newTextViewFrame;
    } else {
		// Keyboard is going away (down) - restore original frame
        textImage.frame = originalTextViewFrame;
		[textLeftImage setFrame:CGRectMake(textLeftImage.frame.origin.x , textLeftImage.frame.origin.y, textLeftImage.frame.size.width, originalTextViewFrame.size.height)];
    }
	
    [UIView commitAnimations];
}

@end
